package com.hyzx.qbasic.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by 黄江华 on 2018/9/11.
 */

public class DateUtils {

    /*
     * 将时间转换为时间戳
     */
    public static long dateToStamp(String s) {
        Date date = parseToDate(s, "yyyy-MM-dd HH:mm:ss");

        if (date != null)
            return date.getTime();

        return 0;
    }

    /*
     * 将时间戳转换为时间
     */
    public static String stampToDate(long ts) {
        Date date = new Date(ts);

        return parseToString(date);
    }

    public static String parseToString(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        return simpleDateFormat.format(date);
    }

    public static String parseToTimeString(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");

        return simpleDateFormat.format(date);
    }

    public static Date parseToDate(String s, String pattern) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);

        try {
            return simpleDateFormat.parse(s);
        } catch (ParseException e) {
        }

        return null;
    }
}
